package com.ali.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ali.ssm.model.User;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: SSM
 * @description:
 * @author: Bruse Queen
 * @create: 2018-05-28 14:54
 **/
@RestController
public class TestController extends BaseController {


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public User test(HttpServletRequest request) throws Exception {
//        JsonObject jsonObject = getJsonObject(request);
//        String email = jsonObject.get("email").getAsString();


        byte[] requestBody = StreamUtils.copyToByteArray(request.getInputStream());
        JSONObject jsonObject = JSON.parseObject((new String(requestBody, "UTF-8")));
        System.out.println(new String(requestBody, "UTF-8"));
//        String email = jsonObject.getString("email");


//        JSONObject jsonObject = JSON.parseObject(getStrFromInsByCode(request.getInputStream(), "utf-8"));
        String email = jsonObject.getString("email");

        User user = new User();
        user.setEmail(email);
        return user;
    }

    @RequestMapping(value = "/test1", method = RequestMethod.POST)
    public User test1(HttpServletRequest request) throws Exception {
        JSONObject jsonObject = JSON.parseObject(getStrFromInsByCode(request.getInputStream(), "utf-8"));
        String email = jsonObject.getString("email");
        User userTemp = new User();
        userTemp.setEmail(email);
        return userTemp;
    }

}
